package cn.exrick.service.impl;

import cn.exrick.mapper.TbOrderReturnExchangeMapper;
import cn.exrick.pojo.TbOrderReturnExchange;
import cn.exrick.pojo.TbOrderReturnExchangeExample;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.service.OrderReturnExchangeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderReturnExchangeServiceImpl implements OrderReturnExchangeService{

    private final static Logger log = LoggerFactory.getLogger(OrderReturnExchangeServiceImpl.class);

    @Autowired
    private TbOrderReturnExchangeMapper orderReturnExchangeMapper;

    @Override
    public DataTablesResult getOrderReturnExchangeList(int draw, int start, int length, Integer type, Integer status,
                                                       String search, String orderCol, String orderDir) {
        DataTablesResult result = new DataTablesResult();
        // 分页
        PageHelper.startPage(start / length + 1, length);
        PageHelper.orderBy(orderCol + " " + orderDir);
        Map<String, Object> paramsMap = new HashMap<>();
        if(null != type){
            paramsMap.put("type", type);
        }
        if(null != status){
            paramsMap.put("status", status);
        }
        if(StringUtils.isNotBlank(search)){
            paramsMap.put("search", search);
        }

        List<TbOrderReturnExchange> list = orderReturnExchangeMapper.selectByPage(paramsMap);
        PageInfo<TbOrderReturnExchange> pageInfo = new PageInfo<>(list);

        result.setRecordsFiltered((int) pageInfo.getTotal());
        result.setRecordsTotal((int) pageInfo.getTotal());

        result.setDraw(draw);
        result.setData(list);
        return result;
    }

    @Override
    public Integer save(TbOrderReturnExchange returnExchange) {
        return orderReturnExchangeMapper.insert(returnExchange);
    }

    @Override
    public TbOrderReturnExchange getByOrderId(String orderId) {
        TbOrderReturnExchangeExample example = new TbOrderReturnExchangeExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        example.setOrderByClause(" created desc ");
        List<TbOrderReturnExchange> list = orderReturnExchangeMapper.selectByExample(example);
        if(list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void deal(Integer id) {
        TbOrderReturnExchange returnExchange = orderReturnExchangeMapper.selectByPrimaryKey(id);
        if(null != returnExchange && returnExchange.getStatus() == 0){
            returnExchange.setStatus(1);
            returnExchange.setUpdated(new Date());
            orderReturnExchangeMapper.updateByPrimaryKey(returnExchange);
        }
    }

    @Override
    public void refuse(Integer id) {
        TbOrderReturnExchange returnExchange = orderReturnExchangeMapper.selectByPrimaryKey(id);
        if(null != returnExchange && returnExchange.getStatus() == 0){
            returnExchange.setStatus(2);
            returnExchange.setUpdated(new Date());
            orderReturnExchangeMapper.updateByPrimaryKey(returnExchange);
        }
    }

}
